/**    
 * @{#} QuestionAdapter.java Create on 2014-5-30    
 *          
 * @author <a href="mailto:evan0502@qq.com">Evan</a>   
 * @version 1.0    
 */
package com.smiier.skin.adapter;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.TextView;
import cn.o.app.ui.OImageView;
import cn.o.app.ui.photo.PhotoActivity;
import cn.o.app.ui.photo.PhotoExtra;
import cn.o.app.util.OUtil;
import cn.skinapp.R;

import com.evan.common.adapter.BaseListAdapter;
import com.evan.common.connection.ConnectionCommon;
import com.lidroid.xutils.BitmapUtils;
import com.smiier.skin.constant.Constant;
import com.smiier.skin.net.entity.Question;
import com.smiier.skin.util.GlobalSettings;
import com.smiier.skin.utils.CommonUtility;

public class QuestionAdapter extends BaseListAdapter {

	protected BitmapUtils mBitmapUtils;

	public QuestionAdapter(Activity activity, ArrayList<Object> array) {
		super(activity, array);
		mBitmapUtils = new BitmapUtils(activity);
	}

	public void setList(ArrayList<Object> array) {
		list = array;
	}

	public QuestionAdapter(Activity activity, ArrayList<Object> array,
			boolean answer) {
		this(activity, array);
	}

	class ViewHolder {
		TextView text_profile, text_content, text_guanzhu_num,
				text_xuanshang_num, text_xuanshang_status, text_date,
				text_doctor_name, text_doctor_title_hospital, text_zhenduan;
		LinearLayout ll_imgs, ll_detail, ll_guanzhu;
		OImageView img_doctor_icon;
		HorizontalScrollView hsv_imgs;
		View ll_detail_line;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup root) {
		ViewHolder viewHolder = null;
		if (CommonUtility.isNull(convertView)) {
			viewHolder = new ViewHolder();
			convertView = inflater.inflate(R.layout.item_question, null);
			viewHolder.text_profile = (TextView) convertView
					.findViewById(R.id.text_profile);
			viewHolder.text_content = (TextView) convertView
					.findViewById(R.id.text_content);
			viewHolder.text_guanzhu_num = (TextView) convertView
					.findViewById(R.id.text_guanzhu_num);
			viewHolder.text_xuanshang_num = (TextView) convertView
					.findViewById(R.id.text_xuanshang_num);
			viewHolder.text_xuanshang_status = (TextView) convertView
					.findViewById(R.id.text_xuanshang_status);
			viewHolder.text_date = (TextView) convertView
					.findViewById(R.id.text_date);
			viewHolder.ll_imgs = (LinearLayout) convertView
					.findViewById(R.id.ll_imgs);
			viewHolder.ll_detail = (LinearLayout) convertView
					.findViewById(R.id.ll_detail);
			viewHolder.img_doctor_icon = (OImageView) convertView
					.findViewById(R.id.img_doctor_icon);
			viewHolder.text_doctor_name = (TextView) convertView
					.findViewById(R.id.text_doctor_name);
			viewHolder.text_doctor_title_hospital = (TextView) convertView
					.findViewById(R.id.text_doctor_title_hospital);
			viewHolder.text_zhenduan = (TextView) convertView
					.findViewById(R.id.text_zhenduan);
			viewHolder.ll_guanzhu = (LinearLayout) convertView
					.findViewById(R.id.ll_guanzhu);
			viewHolder.hsv_imgs = (HorizontalScrollView) convertView
					.findViewById(R.id.hsv_imgs);
			viewHolder.ll_detail_line = convertView
					.findViewById(R.id.ll_detail_line);
			CommonUtility.setViewHolderTag(convertView, viewHolder);
		} else {
			viewHolder = (ViewHolder) CommonUtility
					.getViewHolderTag(convertView);
		}
		try {
			JSONObject object = (JSONObject) getItem(position);
			viewHolder.ll_guanzhu.setVisibility(View.GONE);

			JSONObject question = object.getJSONObject("Question");
			StringBuilder sbprofile = new StringBuilder();
			String sex = CommonUtility.sex.get(question.getInt("Sex"));
			if (!CommonUtility.isNull(sex)) {
				sbprofile.append(sex);
			}
			String age = question.getString("Age");
			if (!CommonUtility.isNull(sbprofile) && !CommonUtility.isNull(age)) {
				sbprofile.append("  |  ").append(age).append("岁");
			}

			int question_status = question.getInt("Status");
			try {
				viewHolder.text_content.setText(question.getString("Content")
						.replace("\n", ""));
			} catch (Exception e) {
				e.printStackTrace();
				viewHolder.text_content.setText(question.getString("Content"));
			}
			viewHolder.text_date.setText(CommonUtility.getTimeDiff(question.getString("CreateTime"),
					//CommonUtility.formatDate(question.getString("CreateTime"),0, 19),
					0, 16));

			viewHolder.text_xuanshang_status.setVisibility(View.VISIBLE);
			int status = question_status;
			/*
			 * if ( == 1) { viewHolder.text_xuanshang_status.setText("待解答");
			 * viewHolder.text_xuanshang_status.setTextColor(0xffdaad29); } else
			 * if (question_status == 2) {
			 * viewHolder.text_xuanshang_status.setText("解答中");
			 * viewHolder.text_xuanshang_status.setTextColor(0xffdaad29); } else
			 * if (question_status == 3) {
			 * viewHolder.text_xuanshang_status.setText("已解答");
			 * viewHolder.text_xuanshang_status.setTextColor(Color.GRAY); } else
			 * { viewHolder.text_xuanshang_status.setVisibility(View.GONE); }
			 */
			int isAppraise = question.getInt("Is_Appraise");
			if (status == Question.STATUS_PRE_ANSWER) {
				viewHolder.text_xuanshang_status.setText("待接单");
				viewHolder.text_xuanshang_status.setTextColor(0xffdaad29);
			} else if (status == Question.STATUS_ANSWERING) {
				viewHolder.text_xuanshang_status.setText("待解答");
				viewHolder.text_xuanshang_status.setTextColor(0xffdaad29);
			} else if (status == Question.STATUS_ANSWERED && isAppraise == 1) {
				viewHolder.text_xuanshang_status.setText("已完成");
				viewHolder.text_xuanshang_status.setTextColor(Color.GRAY);
			} else if (status == Question.STATUS_ANSWERED && isAppraise == 0) {
				viewHolder.text_xuanshang_status.setText("待评价");
				viewHolder.text_xuanshang_status.setTextColor(Color.GRAY);
				// mXuanShangStatusText.setVisibility(View.GONE);
			}
			boolean isTextXuanShangVLine = viewHolder.text_xuanshang_status
					.getVisibility() == View.VISIBLE;
			boolean isTextProfileVLine = false;// 是否需要添加"  |  "

			if (CommonUtility.xuanshang.get(question.getInt("Is_Cost"))) {
				isTextProfileVLine = true;
				viewHolder.text_xuanshang_num.setVisibility(View.VISIBLE);
				viewHolder.text_xuanshang_num.setText("悬赏:￥"
						+ question.getString("Cost")
						+ (isTextXuanShangVLine ? "  |  " : ""));
			} else {
				isTextProfileVLine = isTextXuanShangVLine;
				viewHolder.text_xuanshang_num.setVisibility(View.GONE);
			}

			if (isTextProfileVLine) {
				sbprofile.append("  |  ");
			}
			viewHolder.text_profile.setText(sbprofile);

			if (CommonUtility.isDoctor() && CommonUtility.isCert()) {
				JSONArray pics = question.getJSONArray("Pic");
				viewHolder.ll_imgs.removeAllViews();
				if (pics.length() > 0) {
					viewHolder.hsv_imgs.setVisibility(View.VISIBLE);
					String[] picStrs = new String[pics.length()];
					for (int i = 0; i < pics.length(); i++) {
						OImageView layout = new OImageView(activity);
						layout.setIsRoundRect(true);
						layout.setId(R.id.imgLayout_show);
						layout.setScaleType(ScaleType.CENTER_CROP);
						LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
								(int) OUtil.dp2px(activity, 70),
								(int) OUtil.dp2px(activity, 70));
						layoutParams.setMargins(0, 0,
								(int) OUtil.dp2px(activity, 8), 0);
						layout.setLayoutParams(layoutParams);

						final String path = pics.getString(i) + Constant.IMG_TH;
						mBitmapUtils.display(layout,
								GlobalSettings.SERVER_IMG_URL+path);
						viewHolder.ll_imgs.addView(layout);
						// layout.setImageFileTag(pics.getString(i)
						// + Constant.IMG_SRC);
						layout.setOnClickListener(listener);
						layout.setTag(i);
						layout.setTag(R.id.tag_obj, picStrs);
						picStrs[i] = ConnectionCommon.getInstance().REQUEST_PIC_PATH
								+ pics.getString(i) + Constant.IMG_SRC;
					}
				} else {
					viewHolder.hsv_imgs.setVisibility(View.GONE);
				}
			} else {
				viewHolder.hsv_imgs.setVisibility(View.GONE);
			}

			if (question_status != 1) {
				JSONObject doctor = object.getJSONObject("Doctor_User");
				if (!CommonUtility.isNull(doctor.getString("Name"))) {
					JSONObject answer = object.getJSONObject("Answer");
					viewHolder.ll_detail.setVisibility(View.VISIBLE);
					String iconPath = doctor.getString("Pic");
//					if (iconPath.indexOf(".") == -1) {
						iconPath += Constant.IMG_TH;
//					}
					/*
					 * mBitmapUtils.display(viewHolder.img_doctor_icon,
					 * GlobalSettings.SERVER_IMG_URL + iconPath, new
					 * OBitmapLoadCallBack<View>());
					 */
					CommonUtility.displayHeadImage(viewHolder.img_doctor_icon,
							mBitmapUtils, GlobalSettings.SERVER_IMG_URL
									+ iconPath, convertView.getResources(),
							doctor.getInt("Sex"));
					viewHolder.text_doctor_name.setText(doctor
							.getString("Name"));
					StringBuilder sb = new StringBuilder();
					JSONArray professional = doctor
							.getJSONArray("Professional");
					String tmp = CommonUtility.convertJSONArray2String(
							professional, ",");
					if (tmp != null && !tmp.trim().equals("")) {
						sb.append(" | ");
						sb.append(tmp);
					}
					String hos = doctor.getString("Hospital");
					if (hos != null && !hos.equals("")) {
						sb.append(" | ");
						sb.append(hos);
					}

					viewHolder.text_doctor_title_hospital
							.setText(sb.toString());
					String malady = answer.getString("Malady");
					if (!CommonUtility.isNull(malady)) {
						viewHolder.text_zhenduan.setText("\""
								+ answer.getString("Malady") + "\"");
					} else {
						viewHolder.text_zhenduan.setText("等待输出诊断结果...");
					}
				} else {
					viewHolder.ll_detail.setVisibility(View.GONE);
					viewHolder.ll_detail_line.setVisibility(View.GONE);
				}
			} else {
				viewHolder.ll_detail.setVisibility(View.GONE);
				viewHolder.ll_detail_line.setVisibility(View.GONE);
			}

			convertView.setTag(R.id.tag_obj, object);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return convertView;
	}

	OnClickListener listener = new OnClickListener() {

		@Override
		public void onClick(View view) {
			int position = (Integer) view.getTag();
			String[] pics = (String[]) view.getTag(R.id.tag_obj);
			PhotoExtra extra = new PhotoExtra();
			extra.setDisplayedIndex(position);
			extra.setPhotos(OUtil.asArrayList(pics));
			Intent intent = new Intent(activity, PhotoActivity.class);
			extra.putTo(intent);
			activity.startActivity(intent);
		}
	};
}